/* JUGJobs -- A Jobs Posting Application for Java Users Groups and Other Groups
 * $Id: StrippingHtmlScanner.java,v 1.2 2006/04/15 05:09:51 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the JUGJobs project.
 *
 * The Initial Developers of the Original Code are the members of the Triangle
 * Java User's Group in the RTP area of North Carolina.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developers. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.jugjobs.util;

/**
 * @author BigLee -- Lee Haslup
 * 
 * This concrete subclass of HTML scanner is used to produce a summary
 * version of an HTML text with *all* HTML tags stripped out.
 *
 */
public class StrippingHtmlScanner extends HTMLScanner {

	private boolean problemNoted;
	private StringBuffer summary;
	private int spaceRemaining;

	public StrippingHtmlScanner(String myString) {
		super(myString);
	}

	protected void noteProblem(String problem) throws HTMLScanningException {
		problemNoted = true;

	}

	protected void text(String st) throws HTMLScanningException {
		if(spaceRemaining==-1) {
			summary.append(st);
		}
		else if(spaceRemaining!=0) {
			if(spaceRemaining>=st.length()) {
				summary.append(st);
				spaceRemaining -= st.length();
			}
			else {
				summary.append(st.substring(0,spaceRemaining));
				spaceRemaining=0;
			}
			if(spaceRemaining==0) {
				summary.append("...");
			}
		}
	}

	protected void wicket() throws HTMLScanningException {
		// nothing to do.
	}

	protected void closeWicket() throws HTMLScanningException {
		// nothing to do.
	}

	protected void tag(String tag) throws HTMLScanningException {
		// leave a space for each tag except for bold and italics
		if( !tag.equalsIgnoreCase("i")
		 && !tag.equalsIgnoreCase("/i")
		 && !tag.equalsIgnoreCase("b")
		 && !tag.equalsIgnoreCase("/b")){
			text(" ");
		}
	}

	protected void tagParms(String parms) throws HTMLScanningException {
		// nothing to do.
	}
	
	public String summarize() {
		return summarize(TextAndHtmlUtilities.SUMMARY_SIZE);
	}

	public String summarize(int sumLen) {
		problemNoted = false;
		summary=new StringBuffer();
		spaceRemaining = sumLen;
		try {
			performScan();
		} catch (HTMLScanningException e) {
			problemNoted = true;
		}

		if(problemNoted) {
			summary.append("<br />(?)");
		}
		return summary.toString();
	}

	protected boolean proceedOnError() {
		// if we are done summarizing then a parsing error makes a good place to stop
		return spaceRemaining!=0;
	}

}
